<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Alex
 * Created Time: 2019/6/23 9:03
 * Modified Time: 2019/6/23 9:03
 * Version: 2.0
 */
namespace App\Modules\Member\Services;
use App\Lib\Facades\FLAuth;
use App\Lib\Utilities;
use App\Modules\Employee\Criterias\GetWxLikePortfolioListCriteria;
use App\Modules\Employee\Repositories\PortfolioLikeRepository;
use App\Modules\Employee\Repositories\PortfolioRepository;
use App\Modules\Member\Criterias\getMemberInfoCriteria;
use App\Modules\Member\Criterias\GetWxCoinListCriteria;
use App\Modules\Member\Repositoryies\MemberCoinRepository;
use App\Modules\Member\Repositoryies\MemberRepository;
use App\Modules\Message\Repositories\MessageLogRepository;
use App\Modules\Message\Repositories\MessageRepository;
use App\Modules\Order\Repositories\OrderRepository;
use App\Modules\Store\Criterias\GetMostPopularStoresCriteria;
use App\Modules\Store\Repositories\StoreRepository;

class MemberService
{
    /**
     * @var MemberRepository
     */
    private $memberRepository;
    private $memberCoinRepository;
    private $storeRepository;
    private $portfolioRepository;
    private $portfolioLikeRepository;
    private $orderRepository;
    private $messageLogRepository;
    public function __construct(MemberRepository $memberRepository, MemberCoinRepository $memberCoinRepository,
StoreRepository $storeRepository, PortfolioRepository $portfolioRepository,PortfolioLikeRepository $portfolioLikeRepository,
OrderRepository $orderRepository,MessageLogRepository $messageLogRepository)
    {
        $this->memberRepository = $memberRepository;
        $this->memberCoinRepository = $memberCoinRepository;
        $this->storeRepository = $storeRepository;
        $this->portfolioRepository = $portfolioRepository;
        $this->portfolioLikeRepository =$portfolioLikeRepository;
        $this->orderRepository = $orderRepository;
        $this->messageLogRepository = $messageLogRepository;
    }

	/**
	 * 小程序获取用户基本信息
	 * @param $memberId
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 * @author Happy dong
	 * @modifiedTime 2019-08-12 15:35
	 */
	public function getMemberInfo($memberId){
		return $this->memberRepository->getMemberInfoById($memberId);
	}

	/**
	 * 小程序端
	 * 获取附近人气最高的店铺信息列表
	 * @param $requestData
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getMostPopularStore($adcode,$pageSize){
    	return $this->storeRepository
            ->pushCriteria(new GetMostPopularStoresCriteria($adcode))
            ->paginate($pageSize,['*'],'paginate');

	}


	public function getLikePortfolios($memberId,$pageSize){
		$pageSize = isset($pageSize)?$pageSize:10;
		$store_list = $this->portfolioLikeRepository->pushCriteria(new GetWxLikePortfolioListCriteria($memberId))->paginate($pageSize,['*'],'paginate');
		return $store_list;
	}

	public function getCoinList($requestData){
		$memberId = FLAuth::id();
		$coin = $this->memberRepository->find($memberId,['coin'])->toArray();
		$pageSize = isset($requestData['pageSize'])?$requestData['pageSize']:10;
		$coin_list = $this->memberCoinRepository->pushCriteria(new GetWxCoinListCriteria($memberId))->paginate($pageSize,['*'],'paginate');;
		$coin['list'] = Utilities::dealPage($coin_list);

		return $coin;
	}

	/**
	 * 是否第一次登录
	 * @param $memberId
	 * @return array
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function isFirstLogin($memberId){
		$result = $this->memberRepository->isExisted($memberId);
		$flag = isset($result->is_first) ? $result->is_first : 0;
		return ['status'=>1,'msg'=>'是否第一次登录信息获取成功','data'=>$flag];
	}

	/**
	 * 消息未读数量
	 * @param $memberId
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function  getMessageOrderNum($memberId){
		$list['order_doing'] = $this->orderRepository->getMemberOrderUnfinishedNum($memberId);
//		$list['message_unread'] = $this->messageLogRepository->getUnreadNum($memberId,1);

		$count = $this->messageLogRepository->countMessageList($memberId,1,null,false);
		$count =empty($count->toArray()) ? [] : array_column($count->toArray(),null,'is_read');
		$list['message_unread'] = empty($count) || empty( $count[config('constant.no')]) ? 0 :  $count[config('constant.no')]['count'];
		return $list;
	}

	/**
	 * 获取用户县令币余额
	 * @param $memberId
	 * @return array
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function getMemberCoinBalance($memberId){
		$memberInfo = $this->memberRepository->isExisted($memberId);
		return ['status'=>1,'msg'=>'县令币余额获取成功！','data'=>['coin'=>$memberInfo->coin]];
	}


	/**
	 * 推广送币
	 * @param $openId
	 * @return array
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function addCoinToMemberBySpread($openId){
		$memberInfo = $this->memberRepository->isExisted($openId,false);
		$memberInfo->coin += config('constant.spread_coin_live');
		$memberInfo->save();
		return ['status'=>1,'msg'=>'县令币赠送成功！','data'=>''];
	}

}
